package com.pboc.web;

import com.pboc.base.BaseException;
import com.pboc.base.PageModel;
import com.pboc.entity.SysDept;
import com.pboc.entity.SysWage;
import com.pboc.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;
    @RequestMapping("/getConombox")
    @ResponseBody
    public List<SysDept> getConombox(){
        return sysDeptService.getList(new SysDept());
    }

    @RequestMapping("/getConomboxForAll")
    @ResponseBody
    public List<SysDept> getConomboxForAll(){
        List<SysDept> depts = new ArrayList<SysDept>();
        SysDept dept=new SysDept();
        dept.setDepartName("全部");
        dept.setId(new Integer(-1));
        depts.add(dept);
        depts.addAll(sysDeptService.getList(new SysDept()));
        return depts;
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageModel list(PageModel<SysDept> pageModel, SysDept sysDept, Integer rows, Integer page) {
        pageModel.setPageSize(rows);
        pageModel.setPageNumber(page);
        pageModel.setQueryObj(sysDept);
        sysDeptService.getList(pageModel);
        return pageModel;
    }
    @RequestMapping("view")
    public String view(){
        return "/dept/sysDept_view";
    }
    @RequestMapping("/info")
    public String info(Integer id, Model model){
        if(id!=null){
            model.addAttribute("dept", sysDeptService.getById(id));
        }
        return "/dept/sysDept_info";
    }
    @RequestMapping("/addDept")
    @ResponseBody
    public Map addDept(SysDept dept){
        Map map = new HashMap();
        try {
            sysDeptService.addDept(dept);
            map.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
        }
        return map;

    }
    @RequestMapping("/editDept")
    @ResponseBody
    public Map editDept(SysDept dept){
        Map map = new HashMap();
        try {
            sysDeptService.editDept(dept);
            map.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
        }
        return map;

    }
    @RequestMapping("/delDept")
    @ResponseBody
    public Map delDept(Integer id){
        Map map = new HashMap();
        try{
            sysDeptService.delDept(id);
            map.put("success", true);
        }catch (BaseException e){
            map.put("success", false);
            map.put("mes", e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;

    }

}
